import pygame


class Player:
    def __init__(self):
        # Laura resource loading
        self.HP = 700  # HP
        self.init_HP = 700  # initial HP
        self.player_up = pygame.image.load(r"image/up_Laura.png")
        self.player_down = pygame.image.load(r"image/down_Laura.png")
        self.player_right = pygame.image.load(r"image/right_Laura.png")
        self.player_left = pygame.image.load(r"image/left_Laura.png")
        self.player_death = pygame.image.load(r"image/death.png")
        self.player_head = pygame.image.load(r"image/head.png")
        self.little_head = pygame.image.load(r"image/little_head.png").convert_alpha()

        # animation set
        self.player_attack1_le = pygame.image.load(r"image/le_1.png")
        self.player_attack2_le = pygame.image.load(r"image/le_2.png")
        self.player_attack3_le = pygame.image.load(r"image/le_3.png")
        self.player_attack4_le = pygame.image.load(r"image/le_4.png")

        self.player_attack1_ri = pygame.image.load(r"image/ri_1.png")
        self.player_attack2_ri = pygame.image.load(r"image/ri_2.png")
        self.player_attack3_ri = pygame.image.load(r"image/ri_3.png")
        self.player_attack4_ri = pygame.image.load(r"image/ri_4.png")

        # walking test
        self.walk_flash_times = 40  # walk timer
        self.is_walk = False  # walking state mark
        self.player_left_1 = pygame.image.load(r"image/left_Laura_1.png")
        self.player_left_2 = pygame.image.load(r"image/left_Laura_2.png")
        self.player_left_3 = pygame.image.load(r"image/left_Laura_3.png")
        self.player_left_4 = pygame.image.load(r"image/left_Laura_4.png")

        self.player_right_1 = pygame.image.load(r"image/right_Laura_1.png")
        self.player_right_2 = pygame.image.load(r"image/right_Laura_2.png")
        self.player_right_3 = pygame.image.load(r"image/right_Laura_3.png")
        self.player_right_4 = pygame.image.load(r"image/right_Laura_4.png")

        # upwards
        self.player_up_1 = pygame.image.load(r"image/laura_up_1.png")
        self.player_up_2 = pygame.image.load(r"image/laura_up_2.png")

        # downwards
        self.player_down_1 = pygame.image.load(r"image/down_Laura_1.png")
        self.player_down_2 = pygame.image.load(r"image/down_Laura_2.png")

        self.coord_x = 315
        self.coord_y = 315
        self.player_face = 0  # 0 for up, 1 for down, 2 for right, 3 for left
        self.player_speed_x = 0  # player x speed
        self.player_speed_y = 0  # player y speed
        self.remain_bullet = 30  # remaining bullets number

        # timer for frame-based time dependent effect
        self.remain_buff = 0  # remaining buff time (increase speed)
        self.remain_de_buff = 0  # remaining de_buff time (decrease speed)
        self.freeze_time = 0  # remaining freeze time

        self.alive = True
        self.remain_bar = 0
        self.flash_times = 40  # frame number for animation
        self.is_flash = False  # animation state
        self.melee_damage = 1  # melee damage
        self.flag = True  # special flag for animation state marking

    def move_player(self, room):
        if self.coord_x > 595:
            self.coord_x = 595
        if self.coord_x < 2:
            self.coord_x = 2
        if self.coord_y > 575:
            self.coord_y = 575
        if self.coord_y < 2:
            self.coord_y = 2
        if room.state(self.coord_x + self.player_speed_x, self.coord_y + self.player_speed_y) != 1:
            if room.state(self.coord_x + 30 + self.player_speed_x, self.coord_y + self.player_speed_y) != 1:
                if room.state(self.coord_x + self.player_speed_x, self.coord_y + self.player_speed_y + 50) != 1:
                    if room.state(self.coord_x + 30 + self.player_speed_x,
                                  self.coord_y + self.player_speed_y + 50) != 1:
                        if room.state(self.coord_x + self.player_speed_x, self.coord_y + self.player_speed_y + 20) != 1:
                            if room.state(self.coord_x + 30 + self.player_speed_x,
                                          self.coord_y + self.player_speed_y + 20) != 1:
                                if room.state(self.coord_x + 15 + self.player_speed_x,
                                              self.coord_y + self.player_speed_y) != 1:
                                    if room.state(self.coord_x + 15 + self.player_speed_x,
                                                  self.coord_y + self.player_speed_y + 50) != 1:
                                        if room.state(self.coord_x + self.player_speed_x,
                                                      self.coord_y + self.player_speed_y + 40) != 1:
                                            if room.state(self.coord_x + 30 + self.player_speed_x,
                                                          self.coord_y + self.player_speed_y + 40) != 1:
                                                self.coord_x += self.player_speed_x
                                                self.coord_y += self.player_speed_y

    def damage_player(self, mag):
        if self.alive == 1:
            self.HP -= mag
            if self.HP < 0:
                self.HP = 0
                self.alive = False

    def is_death(self):
        if self.HP == 0:
            self.alive = False

    def position(self):
        if self.coord_y < self.coord_x < 480:
            self.coord_y += 520
            return 0
        elif self.coord_x > self.coord_y and self.coord_x >= 480:
            self.coord_x -= 520
            return 1
        elif self.coord_x < self.coord_y < 480:
            self.coord_x += 520
            return 2
        elif self.coord_x < self.coord_y and self.coord_y >= 480:
            self.coord_y -= 520
            return 3
